package com.rayo.accesscontrollercopy.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.litesuits.common.utils.HexUtil;
import com.rayo.accesscontrollercopy.R;
import com.rayo.wallreader.jni.WallreaderJni;


/**
 * Created by yanghaojie on 2017/3/10.
 */

public class AppUtils {
    public static boolean isNewVersion(String version, Context context) {
        if (version.isEmpty()) return false;
        float v = Float.valueOf(getVersionName(context));
        float nv = Float.valueOf(version);
        return nv > v;
    }

    public static String getVersionName(Context context) {
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return "0.0";
        }
    }

    public static String getVersion(Context context) {
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            StringBuilder builder = new StringBuilder(56);
            builder.append(context.getString(R.string.about_app_name) + " - " + context.getString(R.string.app_name) + "\n");
            builder.append(context.getString(R.string.about_app_version) + " - " + info.versionName);
            return builder.toString();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return context.getString(R.string.data_wrong);
        }
    }
    public static boolean isNetOk(Context context) {
        ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm == null)
            return false;
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo == null) {
            return false;
        }
        if (netInfo.isConnected()) {
            return true;
        }
        return false;
    }

    public static byte[] hexStringToByte(String hex) {
        int len = (hex.length() / 2);
        byte[] result = new byte[len];
        char[] achar = hex.toCharArray();
        for (int i = 0; i < len; i++) {
            int pos = i * 2;
            result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
        }
        return result;
    }

    private static int toByte(char c) {
        byte b = (byte) "0123456789ABCDEF".indexOf(c);
        return b;
    }
    public static byte[] out;
    public static String getDecode (String code){
        out = new byte[512];
        byte[] in1= HexUtil.decodeHex(code.toCharArray());
        int oq =  WallreaderJni.getmInstance().des(in1.length,in1,40,out);
        byte[] buf = new byte[oq];
        System.arraycopy(out,0,buf,0,oq);
        String s=HexUtil.encodeHexStr(buf);
        StringBuffer aa = new StringBuffer();
        for(int i =0;i<s.length()/2;i++){
            String a = s.substring(0,2);
            s = s.substring(2);
            if(!a.equals("00")){
                aa.append(a);
            }else {
                break;
            }
        }
        return new String(aa);
    }
}
